package com.maszy.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.maszy.bean.DoorStore;
import com.maszy.db.DBConnection;

public class DoorStoreService {
	DoorStore service=new DoorStore(); 
	private static PreparedStatement pst;
	private static ResultSet rs = null;
	public  int add(DoorStore obj) {
		DBConnection con = new DBConnection();
		int result = 0;
		String sql ="insert into Maszy_DoorStore(Maszy_DoorStore_Name,Maszy_DoorStore_Adress,Maszy_DoorStore_Sort,Maszy_DoorStore_Remark) values(?,?,?,?)";
		try {
			pst = con.getConnection().prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {			
			pst.setString(1,obj.getDoorStore_Name());
			pst.setString(2, obj.getDoorStore_Adress());
			pst.setInt(3, obj.getDoorStore_Sort());
			pst.setString(4, obj.getDoorStore_Remark());
			result = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;

				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return result;
	}
	public int update(DoorStore obj){
		DBConnection con = new DBConnection();
		int i=0;
		String sql="update Maszy_DoorStore set Maszy_DoorStore_Name=?,Maszy_DoorStore_Sort=?,Maszy_DoorStore_Adress=?,Maszy_DoorStore_Remark=? where Maszy_DoorStore_Id=?";
		try {
			pst = con.getConnection().prepareStatement(sql);			
			pst.setString(1, obj.getDoorStore_Name());
			pst.setInt(2, obj.getDoorStore_Sort());
			pst.setString(3, obj.getDoorStore_Adress());			
			pst.setString(4, obj.getDoorStore_Remark());
			pst.setLong(5, obj.getDoorStore_Id());
			i=pst.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return i;
	}
	public int delete(DoorStore obj){
		DBConnection con = new DBConnection();
		int i=0;
		String sql="delete from  Maszy_DoorStore  where Maszy_DoorStore_Id=?";
		try {
			pst = con.getConnection().prepareStatement(sql);	
			pst.setLong(1, obj.getDoorStore_Id());
			i=pst.executeUpdate();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;

				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return i;
	}
	public DoorStore queryById(DoorStore obj){
		DBConnection con = new DBConnection();
		String sql="select * from Maszy_DoorStore where Maszy_DoorStore_Id=?";
		try {
			pst = con.getConnection().prepareStatement(sql);	
			pst.setLong(1, obj.getDoorStore_Id());
			rs = pst.executeQuery();
			while (rs.next()) {
				obj=new DoorStore();
				obj.setDoorStore_Id(rs.getLong("Maszy_DoorStore_Id"));
				obj.setDoorStore_Name(rs.getString("Maszy_DoorStore_Name"));
				obj.setDoorStore_Adress(rs.getString("Maszy_DoorStore_Adress"));
				obj.setDoorStore_Remark(rs.getString("Maszy_DoorStore_Remark"));	
				obj.setDoorStore_Sort(rs.getInt("Maszy_DoorStore_Sort"));
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return obj;
	}
	public List<DoorStore> queryAll(){
		List<DoorStore> list=new ArrayList<DoorStore>();
		DBConnection con = new DBConnection();
		String sql="select * from Maszy_DoorStore order by Maszy_DoorStore_Sort";
		try {
			pst = con.getConnection().prepareStatement(sql);				
			rs = pst.executeQuery();
	
			while (rs.next()) {
				DoorStore obj=new DoorStore();
				obj.setDoorStore_Id(rs.getLong("Maszy_DoorStore_Id"));
				obj.setDoorStore_Name(rs.getString("Maszy_DoorStore_Name"));
				obj.setDoorStore_Adress(rs.getString("Maszy_DoorStore_Adress"));
				obj.setDoorStore_Remark(rs.getString("Maszy_DoorStore_Remark"));	
				obj.setDoorStore_Sort(rs.getInt("Maszy_DoorStore_Sort"));
				list.add(obj);
			}
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return list;
	}
}
